<template>
  <div class="app-container">
    <el-row :gutter="24" class="mb8">
      <el-col :span="8">
        标签管理
      </el-col>
      <el-col :span="16" style="text-align: right;">
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="$router.go(0)"
          >刷新</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-refresh"
          size="mini"
          @click="$router.go(-1)"
          >返回</el-button
        >
      </el-col>
    </el-row>

    <el-card class="box-card search">
      <div slot="header" class="clearfix">
        <span>添加标签</span>
      </div>
      <div class="text item">
        <el-form
          :model="formData"
          ref="addForm"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="标签名称：" prop="labelName">
            <el-input
              v-model="formData.labelName"
              placeholder="请输入标签名称"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              :disabled="isChecked"
              v-hasPermi="['gwoper:label:add']"
              >添加</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>筛选列表</span>
      </div>
      <div class="text item">
        <el-form
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          label-width="100px"
        >
          <el-form-item label="标签名称：" prop="labelName">
            <el-input
              v-model="queryParams.labelName"
              placeholder="请输入标签名称"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="状态：" prop="status">
            <el-select
              v-model="queryParams.status"
              placeholder="请选择状态"
              clearable
              size="small"
            >
              <el-option
                v-for="(item, index) in statusOptions"
                :key="index"
                :label="item.dictLabel"
                :value="Number(item.dictValue)"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="添加时间：" prop="showBeginTime">
            <el-date-picker
              v-model="queryParams.createTime"
              placeholder="请选择添加时间"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              size="small"
              @change="changeCreateTime"
              clearable
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              v-hasPermi="['gwoper:label:query']"
              >搜索</el-button
            >
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>标签列表</span>
        <el-button
          type="primary"
          size="mini"
          style="float: right;"
          @click="handleExport"
          v-hasPermi="['gwoper:label:export']"
          >导出数据</el-button
        >
      </div>
      <div v-show="isChangeTable" class="text item">
        <div class="changeBtn">
          <el-button
            type="primary"
            size="mini"
            style="float: right;"
            @click="isChangeTable = !isChangeTable"
            >批量管理</el-button
          >
        </div>

        <el-table v-loading="loading" :data="labelList">
          <el-table-column type="index" width="80" align="center" label="编号" />
<!--          <el-table-column label="ID" align="center" prop="id" />-->
          <el-table-column label="标签名称" align="center" prop="labelName" />
          <el-table-column label="添加时间" align="center" prop="createTime" />
          <el-table-column label="状态" align="center" width="100">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="1"
                inactive-value="0"
                @change="handleStatusChange(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['gwoper:label:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                class="btn_close"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['gwoper:label:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="!isChangeTable" class="text item">
        <div class="changeBtn">
          <el-button
            type="primary"
            size="mini"
            style="float: right;"
            @click="isChangeTable = !isChangeTable"
            >完成管理</el-button
          >
          <el-button
            type="primary"
            size="mini"
            style="float: right;margin-right:10px;"
            @click="batchDelete"
            v-hasPermi="['gwoper:label:remove']"
            >批量删除</el-button
          >
        </div>
        <el-table
          v-loading="loading"
          :data="labelList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column type="index" width="80" align="center" label="编号" />
          <el-table-column label="标签名称" align="center" prop="labelName" />
          <el-table-column label="添加时间" align="center" prop="createTime" />
          <el-table-column label="状态" align="center" width="100" :formatter="statusFormat" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['gwoper:label:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                class="btn_close"
                @click="handleDelete(scope.row.id)"
                v-hasPermi="['gwoper:label:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 修改标签配置对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      :before-close="cancel"
      width="500px"
      :close-on-click-modal="true"
      append-to-body
    >
      <el-form ref="editform" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标签名称" prop="labelName">
          <el-input v-model="form.labelName" placeholder="请输入标签名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listLabel,
  getLabel,
  delLabel,
  addLabel,
  updateLabel
} from "@/api/gwoper/label";

export default {
  name: "Label",
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 添加标签参数
      formData: {
        labelName: null
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        labelName: null,
        status: null,
        createTime: null // 添加时间
      },
      statusOptions: [], // 标签状态列表
      isChangeTable: true, // 控制两个表格的切换
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 标签配置表格数据
      labelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        labelName: [
          {
            required: true,
            message: "请输入标签名称",
            trigger: "blur"
          }
        ],
      },
      isChecked: false // 防重复点击
    };
  },
  created() {
    this.getDicts("gwoper_label_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getList();
  },
  methods: {
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    changeCreateTime(val) {
      console.log(val);
    },
    // 切换状态：启用/禁用
    handleStatusChange(row) {
      console.log(row);
      console.log(row.status)
      let text = row.status == 0 ? "禁用" : "启用";
      this.$confirm(`确认要${text}${row.labelName}标签吗?`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          updateLabel(row)
            .then(response => {
              if (response.code != 200) {
                row.status = row.status == 0 ? "1" : "0";
                return this.msgError(response.msg)
              };
              this.msgSuccess(response.msg);
              this.getList();
            })
            .catch(error => {
              row.status = row.status == 0 ? "1" : "0";
              console.log(error);
            });
        })
        .catch(() => {
          row.status = row.status == 0 ? "1" : "0";
          this.msgInfo("取消" + text);
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (
        this.formData.labelName == "" ||
        this.formData.labelName == null ||
        this.formData.labelName == undefined
      ) {
        return this.msgInfo("请输入标签名称");
      }
      if(this.isChecked){
        return
      }
      this.isChecked = true
      addLabel(this.formData).then(response => {
        console.log(response);
        if(response.code != 200) {
          this.msgError(response.msg)
          return this.isChecked = false
        }
        this.formData = {
          labelName: "",
        };
        this.msgSuccess(response.msg);
        this.open = false;
        this.getList();
        this.isChecked = false
      }).catch(error=>{
        this.isChecked = false
        console.log(error)
      });
    },
    // 批量删除
    batchDelete() {
      if (this.ids.length <= 0) {
        return this.msgInfo("请选择需要删除的标签");
      }
      this.handleDelete(this.ids.join(","));
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      const ids = id;
      this.$confirm(
        '是否确认删除标签配置编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          delLabel(ids).then(response => {
            console.log(response);
            if (response.code != 200) return this.msgError(response.msg);
            this.getList();
            this.ids = [];
            this.msgSuccess(response.msg);
          });
        })
        .catch(() => {
          this.msgInfo("取消删除");
        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row;
      this.open = true;
      this.title = "修改标签配置";
      // const id = row.id;
      // getLabel(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改标签配置";
      // });
    },
    // 筛选表单重置
    resetQuery() {
      this.$refs["queryForm"].resetFields()
      this.getList();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      console.log(this.queryParams);
      this.getList();
    },
    /** 查询标签配置列表 */
    getList() {
      this.loading = true;
      listLabel(this.queryParams)
        .then(response => {
          if (response.code != 200) {
            this.loading = false;
            return this.msgError(response.msg);
          }
          this.labelList = response.rows;
          this.labelList.forEach(item=>{
            item.status=item.status.toString()
          })
          console.log(this.labelList)
          this.total = response.total;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
          console.log(error);
        });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.form = {
        labelName: "",
      };
      this.$refs["editform"].resetFields();
    },
    /** 修改提交按钮 */
    submitForm() {
      this.$refs["editform"].validate(valid => {
        if (!valid) return;

        updateLabel(this.form)
          .then(response => {
            if (response.code != 200) return this.msgError(response.msg);
            this.msgSuccess(response.msg);
            this.cancel();
            this.getList();
          })
          .catch(error => {
            console.log(error);
          });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "operation-center/label/export",
        {
          ...this.queryParams
        },
        `标签列表.xlsx`
      );
    }
  }
};
</script>

<style lang="scss">
@import "@/assets/styles/card.scss";
</style>
